//
//  TextSettingViewController.swift
//  NoteBook
//
//  Created by iOS Developer on 2017/4/26.
//  Copyright © 2017年 sunsheen. All rights reserved.
//

import UIKit

class TextSettingViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
    
    @IBOutlet weak var tableView: UITableView!
    
    @IBOutlet weak var resultSetting: UILabel!
    
    @IBOutlet weak var fontSize: UILabel!
    weak var styleDelegate: CustomTextStyleDelegate?
    lazy var familynames: [String] = {
        return UIFont.familyNames.sorted()
    }()
    var fontName: String = "PingFang SC" {
        didSet {
            resultSetting.font = UIFont(name: fontName, size: CGFloat(sizeOfFont))
        }
    }
    var sizeOfFont: CGFloat = 17 {
        didSet{
            fontSize.text = String(describing: sizeOfFont)
            resultSetting.font = UIFont(name: fontName, size: sizeOfFont)
        }
    }
    var fontColor: UIColor = .black {
        didSet {
            resultSetting.textColor = fontColor
        }
    }
    override func viewDidLoad() {
        tableView.delegate = self
        tableView.dataSource = self
    }
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return familynames.count
    }
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "nameCell", for: indexPath)
        cell.textLabel?.text = familynames[indexPath.row]
        return cell
    }
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        fontName = familynames[indexPath.row]
    }
    
    @IBAction func sizeReduce(_ sender: Any) {
        if sizeOfFont > 9 { sizeOfFont -= 1}
    }
    
    @IBAction func sizeAdd(_ sender: Any) {
        if sizeOfFont < 60 { sizeOfFont += 1}
    }
    
    @IBAction func colorSet(_ sender: ColorButton) {
        fontColor = sender.backgroundColor!
    }
    @IBAction func useChoosed(_ sender: Any) {
        styleDelegate?.giveCustomStyle(fontName: fontName,sizeOfFont: sizeOfFont,fontColor: fontColor)
        dismiss(animated: true)
    }
    
}
protocol CustomTextStyleDelegate: class {
    func giveCustomStyle(fontName: String, sizeOfFont: CGFloat, fontColor: UIColor)
}
